package com.raccoonagents.tools.cascet2d.util;

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

/**
 * 
 * @author Kari Kuvaja
 * a custom logger implementation.
 */

public class FWLogger {
	
	private static Set<Level> reportLevels = new HashSet<Level>();
	
	public static void reportNone(){
		reportLevels.clear();
	}
	
	/**
	 * 
	 */
	public static void reportAll(){
		reportLevels.addAll(Arrays.asList(Level.values()));
	}
	/**
	 * 
	 * @param levels
	 */
	public static void setReportLevels(final Level ... levels){
		reportLevels.clear();
		reportLevels.addAll(Arrays.asList(levels));		
	}
	/**
	 * 
	 */
	public static enum Level{
		INFO(0),
		WARNING(1),
		SEVERE(2);
		
		private final int level;
		
		public int getLevel() {
			return level;
		}
		
		private Level(final int level){
			this.level = level;
		}
	}
	
	/**
	 * 
	 * @param message
	 */
	public static void info(final String message){
		final Level level = Level.INFO;
		
		if(!reportLevels.contains(level)){
			return;
		}
		final Exception e = new Exception();
		final StackTraceElement se = e.getStackTrace()[1];
		System.err.println(getHeader(se));
		System.err.println(level.name()+ ": " + message);		
	}

	/**
	 * 
	 * @param message
	 */
	public static void warning(final String message){
		final Level level = Level.WARNING;
		
		if(!reportLevels.contains(level)){
			return;
		}
		final Exception e = new Exception();
		final StackTraceElement se = e.getStackTrace()[1];
		System.err.println(getHeader(se));
		System.err.println(level.name()+ ": " + message);		
	}

	/**
	 * 
	 * @param message
	 */
	public static void severe(final String message){
		final Level level = Level.SEVERE;
		
		if(!reportLevels.contains(level)){
			return;
		}
		final Exception e = new Exception();
		final StackTraceElement se = e.getStackTrace()[1];
		System.err.println(getHeader(se));
		System.err.println(level.name()+ ": " + message);		
	}
	
	/**
	 * 
	 * @param level
	 * @param se
	 * @return
	 */
	private static String getHeader(final StackTraceElement se){
		final String time = Calendar.getInstance().getTime().toGMTString();		
		final String header = time + " " + se.getClassName() + " " + se.getMethodName() ;
		return header;
		
	}
}
